//
//  SpellViewController.m
//  HaoHaiSpell
//
//  Created by haohai on 16/6/15.
//  Copyright © 2016年 海城. All rights reserved.
//

#import "SpellViewController.h"
#import "FirstClassificationCell.h"
#import "SecondClassificationCell.h"
#import "HomepageCell.h"
#import "SearchCell.h"
<<<<<<< HEAD
#import "SpellModel.h"
#import "BHJBuyShoppingViewController.h"

#define k_url @"http://wfx.hostadmin.com.cn/app/app.php"

=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
@interface SpellViewController ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong)UICollectionView *spellCollectionView;//集合视图
@property (nonatomic,strong)NSMutableArray *allDataSource;//数据源
@property (nonatomic,strong)NSMutableDictionary *parameter;//参数
@property (nonatomic,strong)NSMutableArray *titleArr;
@property (nonatomic,strong)NSMutableArray *secondTitleArr;
@property (nonatomic,strong)NSMutableArray *cellArr;
@property (nonatomic,strong)NSArray *id_cellArray;//cell标志符数组
<<<<<<< HEAD
@property (nonatomic,strong)UILabel *selLabel;
@property (nonatomic,strong)UILabel *selectedLabel;

=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
@end

@implementation SpellViewController
#pragma mark － 懒加载
-(NSMutableArray *)allDataSource{

    if (!_allDataSource) {
        _allDataSource=[NSMutableArray new];
    }
    return _allDataSource;
}

-(NSMutableDictionary *)parameter{

    if (!_parameter) {
<<<<<<< HEAD
        _parameter=[NSMutableDictionary dictionaryWithDictionary:@{@"op":@"tuan",@"page_size":@"10",@"verify":@"e10adc3949ba59abbe56e057f20f883e"}];
=======
        _parameter=[NSMutableDictionary dictionaryWithDictionary:@{@"key":@"value"}];
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    }
    return _parameter;
}




-(UICollectionView *)spellCollectionView{
    
    if (!_spellCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//        layout.itemSize = CGSizeMake(kWidth-20, (kHeight-30)/3);
        _spellCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
        _spellCollectionView.backgroundColor=HWColor(245, 245, 245);
        _spellCollectionView.delegate=self;
        _spellCollectionView.dataSource=self;
    }
    return _spellCollectionView;
}

<<<<<<< HEAD

=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
-(NSMutableArray *)titleArr{

    if (!_titleArr) {
        _titleArr = [NSMutableArray arrayWithArray:@[@"综合",@"人气",@"销量",@"价格"]];
    }
    return _titleArr;
}


-(NSMutableArray *)secondTitleArr{

    if (!_secondTitleArr) {
        _secondTitleArr = [NSMutableArray arrayWithArray:@[@"全部",@"服装城",@"零食馆",@"全球购",@"美妆馆",@"数码馆",@"百货馆",@"蔬菜区",@"玩具馆",@"鲜果区",@"超市",@"水果区",@"海鲜",@"1"]];
    }
    return _secondTitleArr;
}

-(NSMutableArray *)cellArr{

    if (!_cellArr) {
        _cellArr = [NSMutableArray arrayWithArray:@[@"SearchCell",@"FirstClassificationCell",@"SecondClassificationCell",@"HomepageCell",]];
    }
    return _cellArr;
}

<<<<<<< HEAD

=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
-(NSArray *)id_cellArray{

    if (!_id_cellArray) {
        _id_cellArray = @[@"searchCell",@"firstCell",@"secondCell",@"HomepageCell"];
    }
    return _id_cellArray;
}

#pragma mark － 自定义
<<<<<<< HEAD
-(void)fetchDataWithUrl:(NSString *)url parameter:(NSDictionary *)parameter{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSArray *contentArray = dic[@"content"];
        for (NSDictionary *mDic in contentArray) {
            SpellModel  *model = [[SpellModel alloc]init];
            [model setValuesForKeysWithDictionary:mDic];
            [self.allDataSource addObject:model];
        }
        [self.spellCollectionView reloadData];
        NSLog(@"请求成功");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
 }


-(void)reloadDataWithItemRow:(NSInteger)row{
    
    switch (row) {
        case 1:{
            [self.parameter setValue:@"click_count" forKey:@"sort"];
        }
            break;
        case 2:{
            [self.parameter setValue:@"sales_num" forKey:@"sort"];
        }
            break;
        case 3:{
            [self.parameter setValue:@"shop_price" forKey:@"sort"];
        }
            break;
        default:
            break;
    }
    [self fetchDataWithUrl:k_url parameter:self.parameter];
}


// 选中Label改变背景色
- (void)selBackgroundColorLabel:(UILabel *)label
{
    [self.selectedLabel setBackgroundColor:[UIColor whiteColor]];
//    self.selLabel.transform = CGAffineTransformIdentity;
     [self.selectedLabel setTextColor:[UIColor blackColor]];
    [label setTextColor:[UIColor redColor]];
    self.selectedLabel = label;
}

- (void)selBackgroundColor:(UILabel *)label
{
    [self.selLabel setBackgroundColor:[UIColor whiteColor]];
    //    self.selLabel.transform = CGAffineTransformIdentity;
    [self.selLabel setTextColor:[UIColor blackColor]];
    [label setBackgroundColor:[UIColor redColor]];
    [label setTextColor:[UIColor whiteColor]];
    self.selLabel = label;
}
=======
-(void)fetchDataWithUrl:(NSString *)url{


    
    
    
    


}






>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
#pragma mark － UISearchBarDelegate


#pragma mark － UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{


    return 4;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    if (section == 0) {
        return 1;
    }else if (section == 1){
    
        return self.titleArr.count;
    }else if (section == 2){
        return self.secondTitleArr.count;
    }else {
<<<<<<< HEAD
      return self.allDataSource.count;
    }
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

//    BaseCell *cell;
    if (indexPath.section == 0) {
        SearchCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"searchCell" forIndexPath:indexPath];
        cell.contentSearch.placeholder = @"请输入要搜索的内容";

=======
      return 5;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

//    BaseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.id_cellArray[indexPath.section] forIndexPath:indexPath]
    if (indexPath.section == 0) {
        SearchCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"searchCell" forIndexPath:indexPath];
        cell.contentSearch.placeholder = @"请输入要搜索的内容";
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
         return cell;
    }else if (indexPath.section == 1){
    
        FirstClassificationCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
        cell.titleLabel.text = self.titleArr[indexPath.row];
<<<<<<< HEAD
        cell.layer.cornerRadius = 12;
        cell.layer.masksToBounds = YES;
        cell.tag = 1000 + indexPath.row;
        if (indexPath.row == 0) {
         [self selBackgroundColor:cell.titleLabel];
        }
=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
         return cell;
    }else if (indexPath.section ==2){
        SecondClassificationCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"secondCell" forIndexPath:indexPath];
        cell.titleLabel.text = self.secondTitleArr[indexPath.row];
<<<<<<< HEAD
        cell.layer.cornerRadius = 10;
        cell.layer.masksToBounds = YES;
        cell.tag = 2000 + indexPath.row;
        if (indexPath.row == 0) {
            
           [self selBackgroundColorLabel:cell.titleLabel];
        }
        return cell;
    }else{
     HomepageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomepageCell" forIndexPath:indexPath];
        SpellModel *model = self.allDataSource[indexPath.row];
        [cell setCellWithModel:model];
=======
        return cell;
    }else{
     HomepageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomepageCell" forIndexPath:indexPath];
        [cell.discountLabel removeFromSuperview];
        [cell.priceLabel removeFromSuperview];
        [cell.cornerImageView removeFromSuperview];
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
        return cell;
    }
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
<<<<<<< HEAD
    if (indexPath.section == 3) {
        BHJBuyShoppingViewController *buyShoppingVC = [[BHJBuyShoppingViewController alloc]init];
        buyShoppingVC.model = self.allDataSource[indexPath.row];
        [self.navigationController pushViewController:buyShoppingVC animated:YES];
    }else if (indexPath.section == 1){
    
        FirstClassificationCell *cell=[collectionView viewWithTag:1000 + indexPath.row];
        [self reloadDataWithItemRow:cell.tag - 1000];
        NSLog(@"%ld",cell.tag);
//        NSLog(@"%@",self.allDataSource);
        [self selBackgroundColor:cell.titleLabel];
    }else if (indexPath.section == 2){
        
        SecondClassificationCell *cell = [collectionView viewWithTag:2000 + indexPath.row];
        [self selBackgroundColorLabel:cell.titleLabel];
    }
=======
    NSLog(@"%ld",(long)indexPath.row);
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
<<<<<<< HEAD
        return CGSizeMake((kWidth - 20), 35);
    }else if (indexPath.section == 1){
        return CGSizeMake((kWidth - 20) / 5, (kHeight - 64)/ 20);
    }else if (indexPath.section == 2){
    return CGSizeMake((kWidth - 20) / 6, (kHeight - 64) / 20);
    }else{
        return CGSizeMake((kWidth - 20), (kHeight - 64) / 2.2);
=======
        return CGSizeMake(kWidth, 45);
    }else if (indexPath.section == 1){
        return CGSizeMake(kWidth / 5, kHeight / 20);
    }else if (indexPath.section == 2){
    return CGSizeMake(kWidth / 6, kHeight / 20);
    }else{
        return CGSizeMake(kWidth, kHeight / 3);
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    }
}



#pragma mark － 视图的生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.spellCollectionView];

    for (int i = 0; i < self.id_cellArray.count; i++) {
      [self.spellCollectionView registerNib:[UINib nibWithNibName:self.cellArr[i] bundle:nil] forCellWithReuseIdentifier:self.id_cellArray[i]];
    }
<<<<<<< HEAD
    // 请求数据
    [self fetchDataWithUrl:k_url parameter:self.parameter];
    
    
 
=======
    
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    
    // Do any additional setup after loading the view from its nib.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{


    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
